package com.atguigu.gulimall.product.controller;

import java.util.*;

import com.atguigu.common.valid.AddGroup;
import com.atguigu.common.valid.ListValue;
import com.atguigu.common.valid.UpdateGroup;
import com.atguigu.common.valid.UpdateStatusGroup;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.product.entity.BrandEntity;
import com.atguigu.gulimall.product.service.BrandService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;

import javax.validation.Valid;


/**
 * 品牌
 *
 * @author longlon
 * @email 3404424145@qq.com
 * @date 2022-07-23 14:28:50
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //   @RequiresPermissions("product:brand:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = brandService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    // @RequiresPermissions("product:brand:info")
    public R info(@PathVariable("brandId") Long brandId) {
        BrandEntity brand = brandService.getById(brandId);
        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("product:brand:save") @Validated(value = {AddGroup.class})
    public R save(@RequestBody  BrandEntity brand) {
       /* if (result.hasErrors()) {
            Map<String, String> map = new HashMap<>();
            // 1,获取参数结果
            result.getFieldErrors().forEach(item -> {
                String message = item.getDefaultMessage();
                String field = item.getField();
                map.put(field, message);
            });
            return R.error().put("data", map);
        } else {
            brand.setShowStatus(1); // 默认显示设置
            brandService.save(brand);
            return R.ok();
        }*/

        //brand.setShowStatus(1); // 默认显示设置
        brandService.save(brand);
        return R.ok();
    }


    /**
     * 修改品牌表,并对冗余字段的一致
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:brand:update") @Validated(value = { UpdateGroup.class})
    public R update(@RequestBody  BrandEntity brand) {
      //  brandService.updateById(brand);
        brandService.updateDetail(brand);
        return R.ok();
    }



    /**
     * 修改状态
     */
    @PostMapping("/update/status")
    //@RequiresPermissions("product:brand:update")
    public R updateStatus(@RequestBody  BrandEntity brand) {
        brandService.updateById(brand);
        return R.ok();
    }




    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:brand:delete")
    public R delete(@RequestBody Long[] brandIds) {
//        List<BrandEntity> brandEntities = new ArrayList<>();
//        for (Long brandId : brandIds) {
//            brandEntities.forEach(brandEntity -> {
//                brandEntity.setBrandId(brandId);
//                brandEntity.setShowStatus(0);
//            });
//        }
//        brandService.updateBatchById(brandEntities);
        brandService.removeByIds(Arrays.asList(brandIds));
        return R.ok();
    }

}
